<?php
/**
 * IG_Web_Element
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Element.php 524 2011-05-24 01:44:20Z mmxie $
 */
class IG_Web_Element
{
    protected $_id;
    protected $_name;
    protected $_attribs;

    /**
     *
     * @param string $spec
     * @param array $options
     */
    public function __construct($spec = null, $options = null)
    {
        if ($options != null && is_array($options)) {
            $this->setAttribs($options);
        }
    }

    /**
     * 获取节点的名称
     *
     * @return string
     */
    public function getName()
    {
        return $this->_name;
    }

    /**
     * 设置节点的名称
     *
     * @param string $name
     * @return IG_Web_Element
     */
    public function setName($name)
    {
        $this->_name = $name;
        return $this;
    }

    /**
     * 获取节点的ID
     *
     * @return string
     */
    public function getId()
    {
        return $this->_id;
    }
    /**
     * 设置节点的ID
     *
     * @param string $id
     * @return IG_Web_Element
     */
    public function setId($id)
    {
        $this->_id = $id;
        return $this;
    }

    /**
     * 设置属性
     *
     * @param string $name
     * @param mixed $value
     * @return IG_Web_Element
     */
    public function setAttrib($name, $value)
    {
        $this->_attribs[$name] = $value;
        return $this;
    }

    /**
     * 设置属性
     *
     * @param array $attribs
     * @return IG_Web_Element
     */
    public function setAttribs($attribs)
    {
        if ($this->_attribs == null) {
            $this->_attribs = $attribs;
        } else {
            $this->_attribs = array_merge($this->_attribs, $attribs);
        }
        return $this;
    }

    /**
     * 通过属性名获取值
     *
     * @param string $name
     * @return mixed
     */
    public function getAttrib($name)
    {
        $name = (string) $name;
        if (isset($this->$name)) {
            return $this->$name;
        }

        return null;
    }

    /**
     * 获取所有属性
     *
     * @return array
     */
    public function getAttribs()
    {
        return $this->_attribs;
    }

    /**
     * 创建HTML属性
     *
     * @param array $attribs
     * @return string
     */
    protected function _htmlAttribs($attribs)
    {
        $xhtml = '';
        foreach ((array) $attribs as $key => $val) {
            $key = escapeshellarg($key);

            if (('on' == substr($key, 0, 2)) || ('constraints' == $key)) {
                if (!is_scalar($val)) {
                    require_once 'Zend/Json.php';
                    $val = Zend_Json::encode($val);
                }
                $val = preg_replace('/"([^"]*)":/', '$1:', $val);
            } else {
                if (is_array($val)) {
                    $val = implode(' ', $val);
                }
                $val = escapeshellarg($val);
            }

            if (strpos($val, '"') !== false) {
                $xhtml .= " $key='$val'";
            } else {
                $xhtml .= " $key=\"$val\"";
            }

        }
        return $xhtml;
    }

    /**
     *
     * @param string $content
     * @return string
     * @throw IG_Web_Exception
     */
    public function render($content)
    {
        require_once 'IG/Web/Exception.php';
        throw new IG_Web_Exception('render() not implemented');
    }
}